package com.hskn.hss.module.permission.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.permission.entity.SysPermission;
import com.hskn.hss.module.permission.mapper.SysPermissionMapper;
import com.hskn.hss.module.permission.service.ISysPermissionService;
import com.hskn.hss.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-04-30
 */
@Service
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission> implements ISysPermissionService {
    @Autowired
    SysPermissionMapper sysPermissionMapper;

    @Override
    public AjaxResult insert(JSONObject params) throws HssException {
        SysPermission sysPermission = params.getObject("data", SysPermission.class);
        sysPermission.setId(StringUtils.uuid());
        int i = sysPermissionMapper.insert(sysPermission);
        boolean flag = i > 0 ? true : false;

        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public AjaxResult update(JSONObject params) throws HssException {
        SysPermission sysPermission = params.getObject("data", SysPermission.class);

        int i = sysPermissionMapper.updateById(sysPermission);
        boolean flag = i > 0 ? true : false;
        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public SysPermission getSysPermissionById(JSONObject params) throws HssException {
        String id = params.getString("id");
        SysPermission sysPermission = sysPermissionMapper.selectById(id);
        return sysPermission;
    }

    @Override
    public List<SysPermission> getSysPermissionList(JSONObject params) throws HssException {
        Map map = new HashMap();
        map = params.getObject("queryparams", Map.class);
        List<SysPermission> sysPermissionList = sysPermissionMapper.selectByMap(map);
        return sysPermissionList;
    }


}
